/*
更新了EMQ连接方式
using System.Collections.Generic;
using System;
using MQTTnet.Client;
using System.Threading.Tasks;
using Shared;
using Shared.SimpleControl;
using MQTTnet;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace SmartHome
{
public static class MqttCommon
{
static string mqttEncryptKey = "";
static string checkGatewayTopicBase64 = "";
static RemoteMACInfo CurRemoteMACInfo = null;
///
/// 手机标识
///
static Guid currentGuid = Guid.NewGuid ();
///
/// 外网的MQTT是否正在连接
///
static object isConnecting = false.ToString ();
///
/// 远程MqttClient
///
public static IMqttClient RemoteMqttClient;
static bool thisShowTip = true;
public static async System.Threading.Tasks.Task Close (bool RemoveRemoteMqttClient = false)
{
try {
if (RemoteMqttClient != null) {
//thisShowTip = true;
await RemoteMqttClient.DisconnectAsync ();
}
if (RemoveRemoteMqttClient) {
RemoteMqttClient = null;
}
CommonPage.IsRemote = false;
} catch { }
}
static DateTime dateTime = DateTime.MinValue;
///
/// 启动远程Mqtt
///
public static async System.Threading.Tasks.Task StartCloudMqtt ()
{
try {
MainPage.tipTime += " Start remote step.1 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
Application.RunOnMainThread (() => {
if (5 < (DateTime.Now - dateTime).TotalSeconds) {
return;
}
//MainPage.Loading.Start (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.Connecting));
dateTime = DateTime.Now;
});
if (!MainPage.LoginUser.IsLogin) {
isConnecting = false.ToString ();
return;
}
while (isConnecting.ToString () == true.ToString ()) {
if (5 < (DateTime.Now - dateTime).TotalSeconds) {
break;
}
await System.Threading.Tasks.Task.Delay (500);
}
lock (isConnecting) {
Console.WriteLine ("mqtt link thread");
if (isConnecting.ToString () == true.ToString ()) {
return;
}
isConnecting = true.ToString ();
if (RemoteMqttClient != null && RemoteMqttClient.IsConnected) {
return;
}
new System.Threading.Thread (async () => {
try {
MainPage.tipTime += " Start remote step.2 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
//断开后重新链接需要重新登录获取连接的密码
var requestObj = new LoginObj () { Account = MainPage.LoginUser.AccountString.ToLower (), Password = MainPage.LoginUser.Password, Company = MainPage.SoftSmsType };
var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
var tempResult = MainPage.RequestHttps ("Login", requestJson, false);
if (tempResult == null) {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
return;
}
var responsePack = tempResult.ResponseData;
var dictrionaryResult = Newtonsoft.Json.JsonConvert.DeserializeObject> (tempResult.ResponseData.ToString ());
var mqttRequestPar = Newtonsoft.Json.JsonConvert.DeserializeObject (tempResult.ResponseData.ToString ());
//还有种情况是同一个ID 有多个设备用这个id连接(会导致中断)
//mqttEncryptKey = dictrionaryResult ["HdlOnMqttKey"]?.ToString ();
var url = dictrionaryResult ["ConnectMqttBrokerLoadSubDomain"]?.ToString ();
var clientId = dictrionaryResult ["ConnectMqttClientId"]?.ToString ();
var username = dictrionaryResult ["ConnectMqttBrokerUserName"]?.ToString ();
var passwordRemote = dictrionaryResult ["ConnectMqttBrokerPwd"]?.ToString ();
if (RemoteMqttClient == null) {
var requestObj3 = new GatewayByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.RegionID };
Console.WriteLine ("Remote mqtt get Region ID : " + UserConfig.Instance.CurrentRegion.RegionID);
var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
var revertObj3 = MainPage.RequestHttps ("GatewayByRegionList", requestJson3, true, false);
if (revertObj3.StateCode == "SUCCESS") {
var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj3.ResponseData.ToString ());
var gatewayList = responseDataObj;
if (gatewayList != null && gatewayList.Count > 0) {
UserConfig.Instance.CurrentRegion.MAC = gatewayList [0].MAC;
UserConfig.Instance.SaveUserConfig ();
Console.WriteLine ("Remote mqtt get Region MAC : " + gatewayList [0].MAC);
}
} else {
Console.WriteLine ("Remote mqtt get Region MAC Erorr !!");
}
//(2)创建Mqtt客户端
RemoteMqttClient = new MqttFactory ().CreateMqttClient ();
//(3)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理
RemoteMqttClient.UseApplicationMessageReceivedHandler (async e => {
if (isConnecting.ToString () == true.ToString ())
isConnecting = false.ToString ();
var aesDecryptTopic = e.ApplicationMessage.Topic;
var aesDecryptPayload = e.ApplicationMessage.Payload;
Console.WriteLine ("Topic={0}", aesDecryptTopic);
if (aesDecryptTopic == $"NotifyBusGateWayInfoChagne/{CurRemoteMACInfo.md5_mac_string}") {//网关上线,需要更新aeskey
//----第二步:读取账号下面的网关列表
var gatewayListUrl = @"https://developer.hdlcontrol.com/Center/Center/GetGatewayPagger"; //App、Buspro软件登录后获取网关列表 http 请求
var gatewayListRequestPar = new RemoteRequestParameters () { Mac = CurRemoteMACInfo.mac, LoginAccessToken = mqttRequestPar.Token, RequestVersion = "RequestVersion1", RequestProtocolType = 0, RequestSource = 1 };
var gatewayListRequestResult = MainPage.RequestHttps ("", Newtonsoft.Json.JsonConvert.SerializeObject (gatewayListRequestPar), false, false, gatewayListUrl);
var gatewayListRequestResult_Obj = Newtonsoft.Json.JsonConvert.DeserializeObject (gatewayListRequestResult.ResponseData.ToString ());
if (gatewayListRequestResult_Obj != null && gatewayListRequestResult_Obj.pageData.Count > 0) {
CurRemoteMACInfo.aesKey = gatewayListRequestResult_Obj.pageData [0].aesKey;
mqttEncryptKey = CurRemoteMACInfo.aesKey;
}
}
if (aesDecryptTopic == "YouIpAndPortNoRecord" || aesDecryptTopic == "DecryptFail") {// --> 你当前的IP及端口在云端不存在,请重新登录连接下!
await Close (true);
} else if (aesDecryptTopic == @"/BeingSqueezedOffline") {
try {
Application.RunOnMainThread (() => {
MainPage.Loading.Start ("");
MainPage.LoginUser.LastTime = DateTime.MinValue;
MainPage.LoginUser.SaveUserInfo ();
Room.Lists.Clear ();
new Shared.SimpleControl.Phone.AccountLogin (MainPage.LoginUser.AccountString.ToLower (), "").Show ();
MainPage.LoginUser.LastTime = System.DateTime.MinValue;
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
if (CommonPage.IsRemote) {
Close (true);
}
var webPush = new service.hdlcontrol.com_push.WebServicePush ();
webPush.DeleteToken_Push (UserConfig.Instance.tokenID);
});
} catch (Exception ex) {
Console.WriteLine (ex.Message);
} finally {
Application.RunOnMainThread (() => {
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
MainPage.Loading.Hide ();
new Alert (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LoggedOnOtherDevices),
Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.Close)).Show ();
});
}
if (!String.IsNullOrEmpty (MainPage.LoginUser.AllVisionRegisterDevUserNameGuid)) {
#if wallon
#else
com.freeview.global.Video.Logout ();
#endif
}
}
else if (aesDecryptTopic == $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/CheckGateway") {
// = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON";
var ss = CommonPage.MyEncodingUTF8.GetString (aesDecryptPayload);
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject (ss);
if (obj == null) {
return;
}
switch (obj.StateCode) {
case "HDLUdpDataForwardServerMqttClientNoOnLine":
case "NoOnline":
case "NetworkAnomaly"://不在线
MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.RemoteFailedGatewayOffline));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
break;
case "NoRecord"://MAC不正确
MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.MACError));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
break;
case "Success":
CommonPage.IsRemote = true;
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = 0xAA69E64A;
Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
});
MainPage.AddTip (UserConfig.Instance.CurrentRegion.RegionName + ":" + Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess));
break;
default:
MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkLoser));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
break;
}
}
else {
if (!string.IsNullOrEmpty (mqttEncryptKey)) {
aesDecryptTopic = Shared.Securitys.EncryptionService.AesDecryptTopic (e.ApplicationMessage.Topic, mqttEncryptKey);
aesDecryptPayload = Shared.Securitys.EncryptionService.AesDecryptPayload (e.ApplicationMessage.Payload, mqttEncryptKey);
} else {
aesDecryptTopic = e.ApplicationMessage.Topic;
aesDecryptPayload = e.ApplicationMessage.Payload;
}
}
var packet = new Packet ();
packet.Bytes = aesDecryptPayload;
packet.Manager ();
});
RemoteMqttClient.UseDisconnectedHandler (e => {
if (thisShowTip) {
if (CommonPage.IsRemote) {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
}
} else {
thisShowTip = true;
}
});
RemoteMqttClient.UseConnectedHandler (async e => {
Console.WriteLine ($"RemoteMqttClient success!!");
if (CurRemoteMACInfo != null) {
if (CurRemoteMACInfo.isValid == "InValid") {
MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.RemoteFailedGatewayOffline));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
} else {
CommonPage.IsRemote = true;
MqttRemoteSend (new byte [] {0 },3);
//Application.RunOnMainThread (() => {
// MainPage.Loading.Hide ();
// Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = 0xAA69E64A;
// Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
//});
//MainPage.AddTip (UserConfig.Instance.CurrentRegion.RegionName + ":" + Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess));
}
}
});
}
if (clientId == null || username == null || passwordRemote == null) {
return;
}
if (tempResult == null) {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
return;
}
//--第一步:获取mqtt链接参数
var mqttInfoUrl = @"https://developer.hdlcontrol.com/Center/Center/GetConnMqttInfo";//获取连接远程云端Emq Mqtt 服务器连接信息
var mqttInfoRequestPar = new RemoteRequestParameters () { LoginAccessToken = mqttRequestPar.Token, RequestVersion = "RequestVersion1", RequestProtocolType = 0, RequestSource = 1 };
var mqttInfoRequestResult = MainPage.RequestHttps ("", Newtonsoft.Json.JsonConvert.SerializeObject (mqttInfoRequestPar), false, false, mqttInfoUrl);
if (mqttInfoRequestResult != null && mqttInfoRequestResult.ResponseData != null) {
try {
var mqttInfoRequestResult_Obj = Newtonsoft.Json.JsonConvert.DeserializeObject (mqttInfoRequestResult.ResponseData.ToString ());
if (mqttInfoRequestResult_Obj != null) {
url = mqttInfoRequestResult_Obj.connEmqDomainPort;
clientId = mqttInfoRequestResult_Obj.connEmqClientId;
username = mqttInfoRequestResult_Obj.connEmqUserName;
passwordRemote = mqttInfoRequestResult_Obj.connEmqPwd;
//----第二步:读取账号下面的网关列表
var gatewayListUrl = @"https://developer.hdlcontrol.com/Center/Center/GetGatewayPagger"; //App、Buspro软件登录后获取网关列表 http 请求
var gatewayListRequestPar = new RemoteRequestParameters () { LoginAccessToken = mqttRequestPar.Token, RequestVersion = "RequestVersion1", RequestProtocolType = 0, RequestSource = 1 };
var gatewayListRequestResult = MainPage.RequestHttps ("", Newtonsoft.Json.JsonConvert.SerializeObject (gatewayListRequestPar), false, false, gatewayListUrl);
var gatewayListRequestResult_Obj = Newtonsoft.Json.JsonConvert.DeserializeObject (gatewayListRequestResult.ResponseData.ToString ());
//--找出是否存在匹配当前住宅的mac,存在再进行远程。
CurRemoteMACInfo = gatewayListRequestResult_Obj.pageData.Find ((obj) => obj.mac == UserConfig.Instance.CurrentRegion.MAC);
if (CurRemoteMACInfo != null) {
CurRemoteMACInfo.LoginAccessToken = mqttRequestPar.Token;
mqttEncryptKey = CurRemoteMACInfo.aesKey;
var options1 = new MQTTnet.Client.Options.MqttClientOptionsBuilder ()
.WithClientId (clientId)
.WithTcpServer (url.Split (':') [1].Substring ("//".Length), int.Parse (url.Split (':') [2]))
.WithCredentials (username, passwordRemote)
.WithCleanSession ()
.WithCommunicationTimeout (new TimeSpan (0, 0, 20))
.Build ();
await Close ();
await RemoteMqttClient.ConnectAsync (options1);
await MqttRemoteSend (new byte [] { 0 }, 1);
await MqttRemoteSend (new byte [] { 0 }, 2);
}
}
} catch { }
}
} catch (Exception ex) {
System.Console.WriteLine ("============>" + ex.Message);
Application.RunOnMainThread (() => {
//MainPage.Loading.Hide ();
//isConnecting = false.ToString ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
#if DEBUG
Alert a = new Alert (isConnecting.ToString (), ex.Message, "Close");
a.Show ();
#endif
if (MqttCommon.RemoteMqttClient != null) {
MqttCommon.RemoteMqttClient.Dispose ();
}
if (MqttCommon.RemoteMqttClient != null) {
MqttCommon.RemoteMqttClient = null;
}
});
} finally {
isConnecting = false.ToString ();
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
MainPage.tipTime += " Start remote step.7 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
});
}
}) { IsBackground = true }.Start ();
}
} catch (Exception ex) {
System.Console.WriteLine ("============>" + ex.Message);
} finally {
isConnecting = false.ToString ();
}
}
///
///
///
/// 附加数据包
/// 操作类型:0=网关控制;1=订阅网关数据;2=订阅网关上线数据
///
public static async Task MqttRemoteSend (byte [] message, int optionType = 0)
{
try {
if (RemoteMqttClient == null || !RemoteMqttClient.IsConnected) {
await StartCloudMqtt ();
}
if (!RemoteMqttClient.IsConnected) {
return;
}
var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/" + UserConfig.Instance.GatewayMAC.Replace (".", "") + @"/" + currentGuid;
switch (optionType) {
case 0:
if (!string.IsNullOrEmpty (mqttEncryptKey)) {
topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/OldON";
} else {
topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/NewON";
}
//base64加密
var messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey);
var m = new MqttApplicationMessage { Topic = topicName, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
await RemoteMqttClient?.PublishAsync (m);
break;
case 1:
topicName = $"/BusGateWayToClient/{CurRemoteMACInfo.macMark}/Common/#";
await RemoteMqttClient?.SubscribeAsync (topicName);
break;
case 2:
var macStr = CurRemoteMACInfo.mac.ToUpper ();
char [] cArrs = macStr.ToCharArray ();
Array.Reverse (cArrs);
var sss = string.Join (string.Empty, cArrs);
using (var provider = new MD5CryptoServiceProvider ()) {
byte [] buffer = provider.ComputeHash (Encoding.Default.GetBytes (sss));
StringBuilder builder = new StringBuilder ();
for (int i = 0; i < buffer.Length; i++) {
builder.Append (buffer [i].ToString ("x2"));
}
CurRemoteMACInfo.md5_mac_string = builder.ToString ().ToUpper ();
}
topicName = $"/NotifyBusGateWayInfoChagne/{CurRemoteMACInfo.md5_mac_string}";
await RemoteMqttClient?.SubscribeAsync (topicName);
break;
case 3:
topicName = $"/ClientToBusGateWay/{CurRemoteMACInfo.macMark}/Common/CheckGateway";
//var topicNameBase64 = Shared.Securitys.EncryptionService.AesEncryptTopic (topicName, mqttEncryptKey);
////BaseSexFourStrToMqttTopic
//var topicNameSend = BaseSexFourStrToMqttTopic (topicNameBase64);
////base64加密主题
//var messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (new byte [] { 2, 1 }, mqttEncryptKey);
//var m = new MqttApplicationMessage { Topic = topicNameSend, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
//await RemoteMqttClient?.PublishAsync (m);
var messageSend1 = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey);
var m1 = new MqttApplicationMessage { Topic = topicName, Payload = messageSend1, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
await RemoteMqttClient?.PublishAsync (m1);
Console.WriteLine ("CheckGateway");
await RemoteMqttClient?.SubscribeAsync (topicName);
break;
}
} catch (Exception e) {
isConnecting = false.ToString ();
}
}
}
}
public class RemoteRequestParameters
{
public string RequestVersion;
public int RequestSource;
public string LoginAccessToken;
public int RequestProtocolType;
public string Mac = "";
public string GroupName = "";
}
public class MqttRemoteInfo
{
public List pageData;
public int pageIndex = 0;
public int pageSize = 10;
public int totalCount = 3;
public int totalPages = 1;
public bool hasPreviousPage = false;
public bool hasNextPage = false;
}
public class MqttInfo
{
public string connEmqDomainPort;
public string connEmqClientId;
public string connEmqUserName;
public string connEmqPwd;
}
public class RemoteMACInfo
{
public string mac;
public string macMark;
public string isValid;
public string aesKey;
public bool isNewBusproGateway;
public string groupName;
public string projectName;
public string userName;
//app自定义数据
public string md5_mac_string;
public string LoginAccessToken;
}
namespace Shared.Securitys
{
public partial class EncryptionService
{
#region 加密
///
/// 加密主题为Base64
///
///
///
///
public static string AesEncryptTopic (string pToEncrypt, string key)
{
if (string.IsNullOrEmpty (pToEncrypt)) return null;
if (string.IsNullOrEmpty (key)) return pToEncrypt;
//需要加密内容的明文流
Byte [] toEncryptArray = Encoding.UTF8.GetBytes (pToEncrypt);
//配置AES加密Key(密钥、向量、模式、填充)
RijndaelManaged rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES加密器对象
ICryptoTransform cTransform = rm.CreateEncryptor ();
//使用AES将明文流转成密文字节数组
Byte [] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
//将AES生成的密文字节数组转成Base64字符串
return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}
///
/// 加密负载为二进制流
///
///
///
///
public static byte [] AesEncryptPayload (byte [] toEncryptArray, string key)
{
if (string.IsNullOrEmpty (key)) return toEncryptArray;
//配置AES加密Key(密钥、向量、模式、填充)
var rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES加密器对象
var cTransform = rm.CreateEncryptor ();
//使用AES将明文流转成密文字节数组
return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
}
#endregion
#region 解密
///
/// 解密主题数据
///
///
///
///
public static string AesDecryptTopic (string pToDecrypt, string key)
{
//AES密文Base64转成字符串
Byte [] toEncryptArray = Convert.FromBase64String (pToDecrypt);
//配置AES加密Key(密钥、向量、模式、填充)
RijndaelManaged rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES解密器对象
ICryptoTransform cTransform = rm.CreateDecryptor ();
//使用AES将密文流转成明文的字节数组
Byte [] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
//转成字符串
return Encoding.UTF8.GetString (resultArray);
}
///
/// 采用Aes解密负载数据
///
///
///
///
public static byte [] AesDecryptPayload (byte [] toEncryptArray, string key)
{
//配置AES加密Key(密钥、向量、模式、填充)
var rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES解密器对象
var cTransform = rm.CreateDecryptor ();
//使用AES将密文流转成明文的字节数组
return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
}
#endregion
}
}
*/
using System.Collections.Generic;
using System;
using MQTTnet.Client;
using System.Threading.Tasks;
using Shared;
using Shared.SimpleControl;
using MQTTnet;
using System.Text;
using System.Security.Cryptography;
namespace SmartHome
{
public static class MqttCommon
{
static string mqttEncryptKey = "";
static string checkGatewayTopicBase64 = "";
///
/// 手机标识
///
static Guid currentGuid = Guid.NewGuid ();
///
/// 外网的MQTT是否正在连接
///
static object isConnecting = false.ToString ();
///
/// 远程MqttClient
///
public static IMqttClient RemoteMqttClient;
static bool thisShowTip = true;
public static async Task Close (bool RemoveRemoteMqttClient = false)
{
try {
if (RemoteMqttClient != null) {
//thisShowTip = true;
await RemoteMqttClient.DisconnectAsync ();
}
if (RemoveRemoteMqttClient) {
RemoteMqttClient = null;
}
CommonPage.IsRemote = false;
Console.WriteLine ("Close Mqtt!!!");
} catch { }
}
static DateTime dateTime = DateTime.MinValue;
static int startCount = 0;
//public static async Task ReSatart ()
//{
// await Close ();
// await RemoteMqttClient.ConnectAsync (options);
//}
///
/// 启动远程Mqtt
///
public static async System.Threading.Tasks.Task StartCloudMqtt ()
{
MainPage.tipTime += " Start remote step.1 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
Application.RunOnMainThread (() => {
if (5 < (DateTime.Now - dateTime).TotalSeconds) {
return;
}
dateTime = DateTime.Now;
});
if (!MainPage.LoginUser.IsLogin) {
isConnecting = false.ToString ();
return;
}
while (isConnecting.ToString () == true.ToString ()) {
if (5 < (DateTime.Now - dateTime).TotalSeconds) {
break;
}
await System.Threading.Tasks.Task.Delay (500);
}
lock (isConnecting) {
if (isConnecting.ToString () == true.ToString ()) {
return;
}
isConnecting = true.ToString ();
if (RemoteMqttClient != null && RemoteMqttClient.IsConnected) {
MqttCheckGateway ();
return;
}
}
new System.Threading.Thread (async () => {
try {
MainPage.tipTime += " Start remote step.2 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
if (RemoteMqttClient == null) {
var requestObj3 = new GatewayByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.RegionID };
var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj3);
var revertObj3 = MainPage.RequestHttps ("GatewayByRegionList", requestJson3, true, false);
if (revertObj3.StateCode == "SUCCESS") {
var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj3.ResponseData.ToString ());
var gatewayList = responseDataObj;
if (gatewayList != null && gatewayList.Count > 0) {
UserConfig.Instance.CurrentRegion.MAC = gatewayList [0].MAC;
UserConfig.Instance.SaveUserConfig ();
}
} else {
}
//(2)创建Mqtt客户端
RemoteMqttClient = new MqttFactory ().CreateMqttClient ();
//(3)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理
RemoteMqttClient.UseApplicationMessageReceivedHandler (async e => {
if (isConnecting.ToString () == true.ToString ())
isConnecting = false.ToString ();
var aesDecryptTopic = e.ApplicationMessage.Topic;
var aesDecryptPayload = e.ApplicationMessage.Payload;
if (aesDecryptTopic == "YouIpAndPortNoRecord" || aesDecryptTopic == "DecryptFail") {// --> 你当前的IP及端口在云端不存在,请重新登录连接下!
await Close (true);
await MqttCheckGateway ();
} else if (aesDecryptTopic == @"/BeingSqueezedOffline") {
try {
Application.RunOnMainThread (() => {
MainPage.Loading.Start ("");
MainPage.LoginUser.LastTime = DateTime.MinValue;
MainPage.LoginUser.SaveUserInfo ();
Room.Lists.Clear ();
new Shared.SimpleControl.Phone.AccountLogin (MainPage.LoginUser.AccountString.ToLower (), "").Show ();
MainPage.LoginUser.LastTime = System.DateTime.MinValue;
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
if (CommonPage.IsRemote) {
SmartHome.MqttCommon.Close (true);
}
var webPush = new service.hdlcontrol.com_push.WebServicePush ();
webPush.DeleteToken_Push (UserConfig.Instance.tokenID);
});
} catch (Exception ex) {
Console.WriteLine (ex.Message);
} finally {
Application.RunOnMainThread (() => {
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.LinkStatusTipColor;
MainPage.Loading.Hide ();
new Alert (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.Tip), Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LoggedOnOtherDevices),
Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.Close)).Show ();
});
}
#if HDL
if (!String.IsNullOrEmpty (MainPage.LoginUser.AllVisionRegisterDevUserNameGuid)) {
com.freeview.global.Video.Logout ();
}
#endif
} else {
aesDecryptTopic = Shared.Securitys.EncryptionService.AesDecryptTopic (e.ApplicationMessage.Topic, mqttEncryptKey);
aesDecryptPayload = Shared.Securitys.EncryptionService.AesDecryptPayload (e.ApplicationMessage.Payload, mqttEncryptKey);
}
if (aesDecryptTopic == @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/CheckGateway/" + UserConfig.Instance.GatewayMAC.Replace (".", "")) {
MainPage.tipTime += " Start remote end : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
var ss = CommonPage.MyEncodingUTF8.GetString (aesDecryptPayload);
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject (ss);
if (obj == null) {
return;
}
switch (obj.StateCode) {
case "HDLUdpDataForwardServerMqttClientNoOnLine":
case "NoOnline":
case "NetworkAnomaly"://不在线
MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.RemoteFailedGatewayOffline));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
break;
case "NoRecord"://MAC不正确
MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.MACError));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
break;
case "Success":
CommonPage.IsRemote = true;
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = 0xAA69E64A;
Shared.SimpleControl.Phone.UserMiddle.ReadAllDeviceStatus ();
});
MainPage.AddTip (UserConfig.Instance.CurrentRegion.RegionName + ":" + Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkSuccess));
break;
default:
MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.LinkLoser));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
break;
}
} else {
var packet = new Packet ();
packet.Bytes = aesDecryptPayload;
packet.Manager ();
}
});
RemoteMqttClient.UseDisconnectedHandler (e => {
//Console.WriteLine ("RemoteMqttClient UseDisconnectedHandler");
if (thisShowTip) {
if (CommonPage.IsRemote) {
//MainPage.AddTip (Language.StringByID (Shared.SimpleControl.R.MyInternationalizationString.RemoteConnectionDisconnected));
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
}
} else {
thisShowTip = true;
}
});
RemoteMqttClient.UseConnectedHandler (async e => {
//Console.WriteLine ("RemoteMqttClient IsRemote");
await MqttCheckGateway ();
});
}
MainPage.tipTime += " Start remote step.3 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
var requestObj = new LoginObj () { Account = MainPage.LoginUser.AccountString.ToLower (), Password = MainPage.LoginUser.Password, Company = MainPage.SoftSmsType };
var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
var tempResult = MainPage.RequestHttps ("Login", requestJson, false);
if (tempResult == null) {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
});
System.Console.WriteLine ($"重新连接远程通讯失败,因为获取新的KEY失败");
return;
}
//var messgae = System.Text.Encoding.UTF8.GetString (tempResult);
MainPage.tipTime += " Start remote step.4 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
var responsePack = tempResult.ResponseData;// Newtonsoft.Json.JsonConvert.DeserializeObject> (messgae);
//if (responsePack == null || responsePack ["ResponseData"] == null) {
// Console.WriteLine ("断开后重新链接需要重新登录获取连接的密码失败");
// return;
//}
var dictrionaryResult = Newtonsoft.Json.JsonConvert.DeserializeObject> (tempResult.ResponseData.ToString ());
//var dictrionaryResult = Newtonsoft.Json.JsonConvert.DeserializeObject> (responsePack ["ResponseData"].ToString ());
//////还有种情况是同一个ID 有多个设备用这个id连接(会导致中断)
mqttEncryptKey = dictrionaryResult ["HdlOnMqttKey"]?.ToString ();
var url = dictrionaryResult ["ConnectMqttBrokerLoadSubDomain"]?.ToString ();
var clientId = dictrionaryResult ["ConnectMqttClientId"]?.ToString ();
var username = dictrionaryResult ["ConnectMqttBrokerUserName"]?.ToString ();
var passwordRemote = dictrionaryResult ["ConnectMqttBrokerPwd"]?.ToString ();
if (clientId == null || username == null || passwordRemote == null) {
return;
}
////(1)连接到云端的Mqtt客户端连接参数
var options = new MQTTnet.Client.Options.MqttClientOptionsBuilder ()
.WithClientId (clientId)
.WithTcpServer (url.Split (':') [1].Substring ("//".Length), int.Parse (url.Split (':') [2]))//
.WithCredentials (username, passwordRemote)
.WithCleanSession ()
.WithCommunicationTimeout (new TimeSpan (0, 0, 10))
.Build ();
MainPage.tipTime += " Start remote step.5 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
await Close ();
await RemoteMqttClient.ConnectAsync (options);
MainPage.tipTime += " Start remote step.6 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
} catch (Exception ex) {
System.Console.WriteLine ("============>" + ex.Message);
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
isConnecting = false.ToString ();
Shared.SimpleControl.Phone.UserMiddle.LinkStatusTip.BackgroundColor = SkinStyle.Current.DelColor;
#if DEBUG
Alert a = new Alert (isConnecting.ToString (), ex.Message, "Close");
a.Show ();
#endif
if (MqttCommon.RemoteMqttClient != null) {
MqttCommon.RemoteMqttClient.Dispose ();
}
if (MqttCommon.RemoteMqttClient != null) {
MqttCommon.RemoteMqttClient = null;
}
});
} finally {
isConnecting = false.ToString ();
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
MainPage.tipTime += " Start remote step.7 : " + DateTime.Now.ToString ("yyyy-MM-dd hh:mm:ss:ffff") + ";\n";
});
}
}) { IsBackground = true }.Start ();
}
///
/// Logins the by PWDA sync.
///
public static async System.Threading.Tasks.Task LoginByPWDAsync (string account, string password, string source = "", int company = 0)
{
try {
var result = await MainPage.RequestHttpsResultAsync ("Login",
System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (new Dictionary {
["Account"] = account,
["Password"] = password,
["Source"] = source,
["Company"] = company
})));
return result;
} catch {
return null;
}
}
///
/// 将Base64字符串,转换成合法的Mqtt主题
///
///
///
public static string BaseSexFourStrToMqttTopic (string baseSexFourEncStr)
{
baseSexFourEncStr = baseSexFourEncStr.Replace ("+", "[[$-MQTT_PLUS_SYMBOL_REPLACE-$]]");
baseSexFourEncStr = baseSexFourEncStr.Replace ("/", "[[$-MQTT_TILT_SYMBOL_REPLACE-$]]");
return baseSexFourEncStr;
}
public static async System.Threading.Tasks.Task MqttRemoteSend (byte [] message)
{
try {
if (RemoteMqttClient == null || !RemoteMqttClient.IsConnected) {
await StartCloudMqtt ();
}
if (!RemoteMqttClient.IsConnected) {
return;
}
var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/" + UserConfig.Instance.GatewayMAC.Replace (".", "") + @"/" + currentGuid;
//base64加密主题
checkGatewayTopicBase64 = Shared.Securitys.EncryptionService.AesEncryptTopic (topicName, mqttEncryptKey);
//BaseSexFourStrToMqttTopic
var topicNameSend = BaseSexFourStrToMqttTopic (checkGatewayTopicBase64);
//base64加密主体
var messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey);
var m = new MqttApplicationMessage { Topic = topicNameSend, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
await RemoteMqttClient?.PublishAsync (m);
} catch (Exception e) {
isConnecting = false.ToString ();
}
}
public static async Task MqttCheckGateway ()
{
try {
if (RemoteMqttClient == null || !RemoteMqttClient.IsConnected) {
await StartCloudMqtt ();
}
if (!RemoteMqttClient.IsConnected) {
return;
}
var topicName = @"/" + MainPage.LoginUser.AccountString.ToLower () + @"/CheckGateway/" + UserConfig.Instance.GatewayMAC.Replace (".", "");
Console.WriteLine ("MqttCheckGateway : " + topicName);
//base64加密主题
var topicNameBase64 = Shared.Securitys.EncryptionService.AesEncryptTopic (topicName, mqttEncryptKey);
//BaseSexFourStrToMqttTopic
var topicNameSend = BaseSexFourStrToMqttTopic (topicNameBase64);
//base64加密主题
var messageSend = Shared.Securitys.EncryptionService.AesEncryptPayload (new byte [] { 2, 1 }, mqttEncryptKey);
var m = new MqttApplicationMessage { Topic = topicNameSend, Payload = messageSend, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce };
await RemoteMqttClient?.PublishAsync (m);
} catch (Exception e) {
isConnecting = false.ToString ();
}
}
}
}
namespace Shared.Securitys
{
public partial class EncryptionService
{
#region 加密
///
/// 加密主题为Base64
///
///
///
///
public static string AesEncryptTopic (string pToEncrypt, string key)
{
if (string.IsNullOrEmpty (pToEncrypt)) return null;
//需要加密内容的明文流
Byte [] toEncryptArray = Encoding.UTF8.GetBytes (pToEncrypt);
//配置AES加密Key(密钥、向量、模式、填充)
RijndaelManaged rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES加密器对象
ICryptoTransform cTransform = rm.CreateEncryptor ();
//使用AES将明文流转成密文字节数组
Byte [] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
//将AES生成的密文字节数组转成Base64字符串
return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}
///
/// 加密负载为二进制流
///
///
///
///
public static byte [] AesEncryptPayload (byte [] toEncryptArray, string key)
{
//配置AES加密Key(密钥、向量、模式、填充)
var rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES加密器对象
var cTransform = rm.CreateEncryptor ();
//使用AES将明文流转成密文字节数组
return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
}
#endregion
#region 解密
///
/// 解密主题数据
///
///
///
///
public static string AesDecryptTopic (string pToDecrypt, string key)
{
//AES密文Base64转成字符串
Byte [] toEncryptArray = Convert.FromBase64String (pToDecrypt);
//配置AES加密Key(密钥、向量、模式、填充)
RijndaelManaged rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES解密器对象
ICryptoTransform cTransform = rm.CreateDecryptor ();
//使用AES将密文流转成明文的字节数组
Byte [] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
//转成字符串
return Encoding.UTF8.GetString (resultArray);
}
///
/// 采用Aes解密负载数据
///
///
///
///
public static byte [] AesDecryptPayload (byte [] toEncryptArray, string key)
{
//配置AES加密Key(密钥、向量、模式、填充)
var rm = new RijndaelManaged {
Key = Encoding.UTF8.GetBytes (key),
IV = Encoding.UTF8.GetBytes (key),
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
//创建AES解密器对象
var cTransform = rm.CreateDecryptor ();
//使用AES将密文流转成明文的字节数组
return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
}
#endregion
}
}